﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecisionTree
{
    public static class Extensions
    {
        public static int MaxIndex(this int[] sequence)
        {
            int maxIndex = 0;
            int maxValue = sequence[0];

            for (int i = 1; i < sequence.Length; i++)
            {
                if (sequence[i] > maxValue)
                {
                    maxValue = sequence[i];
                    maxIndex = i;
                }
            }

            return maxIndex;
        }
        
        public static int MaxIndex(this double[] sequence)
        {
            int maxIndex = 0;
            double maxValue = sequence[0];

            for (int i = 1; i < sequence.Length; i++)
            {
                if (sequence[i] > maxValue)
                {
                    maxValue = sequence[i];
                    maxIndex = i;
                }
            }

            return maxIndex;
        }

        public static int MaxIndex(this byte[] sequence)
        {
            int maxIndex = 0;
            byte maxValue = sequence[0];

            for (int i = 1; i < sequence.Length; i++)
            {
                if (sequence[i] > maxValue)
                {
                    maxValue = sequence[i];
                    maxIndex = i;
                }
            }

            return maxIndex;
        }

        public static byte[][] Submatrix(this byte[][] matrix, int[] selected)
        {
            byte[][] rVal = new byte[selected.Length][];

            for (int i = 0; i < rVal.Length; i++)
            {
                rVal[i] = matrix[selected[i]];
            }
                
            return rVal;
        }

        public static byte[] Submatrix(this byte[] matrix, int[] selected)
        {
            byte[] rVal = new byte[selected.Length];

            for (int i = 0; i < rVal.Length; i++)
            {
                rVal[i] = matrix[selected[i]];
            }

            return rVal;
        }

        public static void WriteImage(this double[] vector, string path)
        {
            int index = 0;
            Image img = new Image(28, 28);

            for (int i = 0; i < 28; i++)
            {
                for (int j = 0; j < 28; j++)
                {
                    img.SetPixel(i, j, vector[index]);
                    index++;
                }
            }

            img.WriteImage(path);
        }

        public static void WriteImage(this byte[] vector, string path)
        {
            int index = 0;
            Image img = new Image(28, 28);

            for (int i = 0; i < 28; i++)
            {
                for (int j = 0; j < 28; j++)
                {
                    img.SetPixel(i, j, vector[index]);
                    index++;
                }
            }

            img.WriteImage(path);
        }

        public static double StandardDeviation(this List<int> distances)
        {
            double rVal = 0;
            double average = distances.Average();
            foreach (var item in distances)
            {
                rVal += Math.Pow(average - item, 2);
            }
            return Math.Sqrt(rVal / distances.Count);    
        }

        public static byte GetPixel(this byte[] array, int x, int y)
        {
            int numCols = 28;

            return array[x + y * numCols];
        }

        public static void SetPixel(this byte[] array, int x, int y, byte value)
        {
            int numCols = 28;

            array[x + y * numCols] = value;
        }
    }
}
